<?php
namespace app\admin\controller;

use app\common\model\ArticleCategory as ArticleCategoryModel;
use app\common\controller\AdminBase;
use think\Config;
use think\Db;

/**
 * 资源来源管理
 * Class AdminUser
 * @package app\admin\controller
 */
class ArticleCategory extends AdminBase
{
    protected $model;

    protected function _initialize()
    {
        parent::_initialize();
        $this->model = new ArticleCategoryModel;
        $category_level_list  = $this->model->catetree();
        $this->assign('category_level_list', $category_level_list);
    }

    /**
     * 资源来源管理
     * @param string $keyword
     * @param int    $page
     * @return mixed
     */
    public function index()
    {
        $list = $this->model->order('sort DESC')->select(10);
        return $this->fetch('index', ['list' => $list]);
    }

    public function toggle($id, $status, $name)
    {
        if ($this->request->isGet()) {
            if ($this->model->where('id', $id)->update([$name => $status]) !== false) {
                return json(array('code' => 200, 'msg' => '更新成功'));
            } else {
                return json(array('code' => 0, 'msg' => '更新失败'));
            }
        }

    }
    /**
     * 添加资源来源
     * @return mixed
     */
    public function add()
    {
        return $this->fetch();
    }

    /**
     * 保存资源来源
     */
    public function save()
    {
        if ($this->request->isPost()) {
            $data            = $this->request->post();
            $data['status']=1;
            if ($this->model->allowField(true)->save($data)) {
                return json(array('code' => 200, 'msg' => '添加成功'));
            } else {
                return json(array('code' => 0, 'msg' => '添加失败'));
            }

        }
    }

    /**
     * 编辑资源来源
     * @param $id
     * @return mixed
     */
    public function edit($id)
    {
        $info = $this->model->find($id);
        return $this->fetch('edit', ['info' => $info]);
    }

    /**
     * 更新资源来源
     * @param $id
     */
    public function update()
    {

        if ($this->request->isPost()) {
            $data            = $this->request->param();
            if ($this->model->allowField(true)->save($data, ['id' => $data['id']])) {
                return json(array('code' => 200, 'msg' => '修改成功'));
            } else {
                return json(array('code' => 0, 'msg' => '修改失败'));
            }
        }
    }

    public function alldelete()
    {
        $params = input('post.');
        $ids = implode(',', $params['ids']);
        $result = $this->model->destroy($ids);
        if ($result) {
            return json(array('code' => 200, 'msg' => '删除成功'));
        } else {
            return json(array('code' => 0, 'msg' => '删除失败'));
        }
    }
    /**
     * 删除资源来源
     * @param $id
     */
    public function delete($id)
    {
        if ($this->model->destroy($id)) {
            return json(array('code' => 200, 'msg' => '删除成功'));
        } else {
            return json(array('code' => 0, 'msg' => '删除失败'));
        }
    }
}